import Vue from 'vue'
import VueRouter from 'vue-router'
const Login = () =>
    import ('../views/Login.vue')
const Home = () =>
    import ('../views/Home.vue')
const Welcome = () =>
    import ('../components/Home/Welcome/Welcome.vue')
const Users = () =>
    import ('../components/Home/User/Users.vue')
const Rights = () =>
    import ('../components/Home/Power/Rights.vue')
const Roles = () =>
    import ('../components/Home/Power/Roles.vue')
const Cate = () =>
    import ('../components/Home/Goods/Cate.vue')
const Params = () =>
    import ('../components/Home/Goods/Params.vue')
const GoodList = () =>
    import ('../components/Home/Goods/GoodList.vue')
const AddGood = () =>
    import ('../components/Home/Goods/AddGood.vue')
const Orders = () =>
    import ('../components/Home/Orders/Orders.vue')
const Reports = () =>
    import ('../components/Home/Reports/Reports.vue')
Vue.use(VueRouter)

const routes = [{
        path: '/login',
        component: Login
    },
    {
        path: '/home',
        component: Home,
        children: [{
                path: '/',
                redirect: '/welcome',
            },
            {
                path: '/welcome',
                component: Welcome
            },
            {
                path: '/users',
                component: Users
            },
            {
                path: '/rights',
                component: Rights
            },
            {
                path: '/roles',
                component: Roles
            },
            {
                path: '/categories',
                component: Cate
            },
            {
                path: '/params',
                component: Params
            },
            {
                path: '/goods',
                component: GoodList
            },
            {
                path: '/goods/add',
                component: AddGood
            },

            {
                path: '/orders',
                component: Orders
            },
            {
                path: '/reports',
                component: Reports
            }
        ]
    },
    {
        path: '/',
        redirect: '/login'
    }
]

const router = new VueRouter({
    routes
})

router.beforeEach((to, from, next) => {
    //如果是访问登录页直接放行
    if (to.path === '/login') {
        return next()
    }
    //如果是其他页面就需要检测有没有token
    const token = window.sessionStorage.getItem('token')
    if (!token) {
        return next('/login')
    }
    next()
})

export default router